var app = getApp();

Page({
  data: {
    isLoading: false,
    isNoData: true,
    articleList: []
  },
  onLoad: function() {
    var that = this;
    wx.showLoading({
      title: '加载中',
    });
    var hasLogin = wx.getStorageSync('isLogined');
    if (!hasLogin) {
      wx.redirectTo({
        url: "welcome/welcome"
      })
    }
    this.getArtList();
  },
  // 下拉刷新
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画。
    this.getArtList(); //重新获取文章列表
    wx.hideNavigationBarLoading(); //隐藏导航条加载动画。
    wx.stopPullDownRefresh(); //停止当前页面下拉刷新。
  },
  // 获取文章列表
  getArtList: function() {
    var that = this;
    wx.request({
      url: app.globalData.apiBaseUrl + '/medicalPopular/selectMedicalPopularListInfo',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'FilterToken_MT': wx.getStorageSync("token")
      },
      method: 'GET',
      dataType: 'json',
      success: function(msg) {
        msg = msg.data;
        if (msg.code == 200) {
          that.setData({
            articleList: msg.data,
            isLoading: true
          })
          if (msg.data.length > 0) {
            that.setData({
              isNoData: true
            })
          } else {
            that.setData({
              isNoData: false
            })
          }
          wx.hideLoading();
        } else if (msg.code == 501) {
          wx.hideLoading();
          wx.showToast({
            title: msg.msg,
            icon: 'none',
            duration: 3000
          });
          wx.clearStorageSync();
          wx.redirectTo({
            url: "welcome/welcome"
          });
        } else {
          wx.hideLoading();
          wx.showToast({
            title: msg.msg,
            icon: 'none',
            duration: 3000
          })
        }
      }
    })
  },
  // 退出登录
  handleLogout: function() {
    // 清除本地数据
    wx.clearStorageSync();
    wx.redirectTo({
      url: "welcome/welcome"
    })
  }
})